package ar.com.sgsvm.dao.sqlite;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ar.com.sgsvm.dao.IFacturaDao;
import ar.com.sgsvm.dao.SqliteDaoFactory;
import ar.com.sgsvm.entity.Factura;
import ar.com.sgsvm.exceptions.NonexistentEntityException;

public class FacturaSqliteDao implements IFacturaDao {

	@Override
	public Long create(Factura factura) throws Exception {
		ContentValues contentValues = new ContentValues();
		contentValues.put("idCliente", factura.getCliente().getIdCliente());
		final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		contentValues.put("fechaHora", parser.format(factura.getFechaHora()));
		contentValues.put("pagado", factura.getPagado());
		contentValues.put("entregado", factura.getEntregado());
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		return sqliteDaoFactory.open().insert("Factura", null, contentValues);
	}

	@Override
	public List<Factura> readAll() throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("Factura", null, null, null, null, null, null);
		List<Factura> facturas = new ArrayList<Factura>();
		if (cursor.moveToFirst()) {
			do {
				Factura factura = new Factura();
				factura.setIdFactura(cursor.getLong(cursor.getColumnIndex("idFactura")));
				factura.setCliente(sqliteDaoFactory.getClienteDao().readById(cursor.getLong(cursor.getColumnIndex("idCliente"))));
				final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
				factura.setFechaHora(parser.parse(cursor.getString(cursor.getColumnIndex("fechaHora"))));
				factura.setPagado(Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex("pagado"))));
				factura.setEntregado(Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex("entregado"))));
				facturas.add(factura);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return facturas;
	}

	@Override
	public Factura readById(Long idFactura) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("Factura", null, "idFactura = ?", new String[] { String.valueOf(idFactura) }, null, null, null);
		if (cursor.moveToFirst()) {
			Factura factura = new Factura();
			factura.setIdFactura(idFactura);
			factura.setCliente(sqliteDaoFactory.getClienteDao().readById(cursor.getLong(cursor.getColumnIndex("idCliente"))));
			final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
			factura.setFechaHora(parser.parse(cursor.getString(cursor.getColumnIndex("fechaHora"))));
			Boolean pagado = cursor.getInt(cursor.getColumnIndex("pagado"))>0 ? true : false;
			factura.setPagado(pagado);
			Boolean entregado = cursor.getInt(cursor.getColumnIndex("entregado"))>0 ? true : false;
			factura.setEntregado(entregado);
			cursor.close();
			return factura;
		} else {
			throw new NonexistentEntityException("No existe la factura con id " + String.valueOf(idFactura));
		}
	}

	@Override
	public void update(Factura factura) throws Exception {
		ContentValues contentValues = new ContentValues();
		contentValues.put("idCliente", factura.getCliente().getIdCliente());
		final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
		contentValues.put("fechaHora", parser.format(factura.getFechaHora()));
		contentValues.put("pagado", factura.getPagado());
		contentValues.put("entregado", factura.getEntregado());
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		sqliteDaoFactory.open().update("Factura", contentValues, "idFactura = ?", new String[] { String.valueOf(factura.getIdFactura()) });
	}

	@Override
	public void delete(Factura factura) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		sqliteDaoFactory.open().delete("Factura", "idFactura = ?", new String[] { String.valueOf(factura.getIdFactura()) });
	}

	@Override
	public Factura readByIdLineaFactura(long idLineaFactura) throws Exception {
		SqliteDaoFactory sqliteDaoFactory = new SqliteDaoFactory();
		SQLiteDatabase sqliteDatabase = sqliteDaoFactory.open();
		Cursor cursor = sqliteDatabase.query("LineaFactura", null, "idLineaFactura = ?", new String[] { String.valueOf(idLineaFactura) }, null, null, null);
		cursor.moveToFirst();
		Factura factura = readById(cursor.getLong(cursor.getColumnIndex("idFactura")));
		cursor.close();
		return factura;
	}

}
